<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('设备信息列表')" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>IMEI号：</label>
                                <input type="text" name="deviceImei"/>
                            </li>
                            <li>
                                <label>设备类型：</label>
                                <select name="deviceType" class="form-control">
                                    <option value="">所有</option>
                                    <option value="DTU">DTU模组</option>
                                    <option value="4G">4G模组</option>
                                </select>
                            </li>
                            <li>
                                <label>运营商：</label>
                                <select name="deviceOperator" class="form-control" th:with="type=${@dict.getType('operator_type')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>在线状态：</label>
                                <select name="onlineStatus" class="form-control">
                                    <option value="">所有</option>
                                    <option value="online">在线</option>
                                    <option value="offline">离线</option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="basic:device:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="basic:device:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="basic:device:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <div class="btn-group">
                    <button data-toggle="dropdown" class="btn btn-default dropdown-toggle">操作 <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu dropdown-user">
                        <li>
                            <a class="btn btn-default multiple disabled" onclick="getinfo()" shiro:hasPermission="basic:device:getinfo">
                                <i class="fa fa-edit"></i> 获取设备信息
                            </a>
                        </li>
                        <li>
                            <a class="btn btn-default multiple disabled" onclick="getserver()" shiro:hasPermission="basic:device:getserver">
                                <i class="fa fa-edit"></i> 读通道地址
                            </a>
                        </li>
                        <li>
                            <a class="btn btn-default multiple disabled" onclick="setserver()" shiro:hasPermission="basic:device:setserver">
                                <i class="fa fa-edit"></i> 修通道地址
                            </a>
                        </li>
                        <li>
                            <a class="btn btn-default multiple disabled" onclick="offserver()" shiro:hasPermission="basic:device:offserver">
                                <i class="fa fa-edit"></i> 关闭通道
                            </a>
                        </li>
                        <li>
                            <a class="btn btn-default multiple disabled" onclick="upgrade()" shiro:hasPermission="basic:device:upgrade">
                                <i class="fa fa-edit"></i> 版本升级
                            </a>
                        </li>
                    </ul>
                </div>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: select2-js" />
    <th:block th:include="include :: bootstrap-table-fixed-columns-js" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('basic:device:edit')}]];
        var removeFlag = [[${@permission.hasPermi('basic:device:remove')}]];
        var prefix = ctx + "basic/device";

        var datas = [[${@dict.getType('operator_type')}]];

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                importUrl: prefix + "/importData",
                importTemplateUrl: prefix + "/importTemplate",
                sortName: "onlineTime",
                sortOrder: "desc",
                fixedColumns: true,
                fixedNumber: 2,
                fixedRightNumber: 2,
                modalName: "采集设备信息",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'deviceId',
                    title: '设备id',
                    visible: false
                },
                {
                    field: 'deviceImei',
                    title: 'IMEI号',
                    sortable: true
                },
                {
                    field: 'deviceImsi',
                    title: 'IMSI号',
                    sortable: true
                },
                {
                    field: 'deviceIccid',
                    title: 'ICCID号',
                    sortable: true
                },
                {
                    field: 'deviceType',
                    title: '设备类型',
                    sortable: true
                },
                {
                    field: 'deviceOperator',
                    title: '运营商',
                    sortable: true,
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(datas, row.deviceOperator);
                    }
                },
                {
                    field: 'serverA',
                    title: '通道A地址',
                    sortable: true
                },
                {
                    field: 'serverB',
                    title: '通道B地址',
                    sortable: true
                },
                {
                    field: 'serverC',
                    title: '通道C地址',
                    sortable: true
                },
                {
                    field: 'deviceVer',
                    title: '版本号',
                    sortable: true
                },
                {
                    field: 'onlineTime',
                    title: '上线时间',
                    sortable: true
                },
                {
                    field: 'offlineTime',
                    title: '离线时间',
                    sortable: true
                },
                {
                    field: 'deviceCsq',
                    title: '信号强度',
                    align: 'center',
                    sortable: true,
                    formatter: function(value, row, index) {
                        if(value >= 26 && value <= 31){
                            return '<img width="20px;" height="20px;" src="/img/signal_5.jpg">';
                        } else if(value >= 21 && value <= 25) {
                            return '<img width="20px;" height="20px;" src="/img/signal_4.jpg">';
                        } else if(value >= 17 && value <= 20) {
                            return '<img width="20px;" height="20px;" src="/img/signal_3.jpg">';
                        } else if(value >= 13 && value <= 16) {
                            return '<img width="20px;" height="20px;" src="/img/signal_2.jpg">';
                        } else if(value >= 9 && value <= 12) {
                            return '<img width="20px;" height="20px;" src="/img/signal_1.jpg">';
                        } else {
                            return '<img width="20px;" height="20px;" src="/img/signal_0.jpg">';
                        }
                    }
                },
                {
                    field: 'onlineStatus',
                    title: '在线状态',
                    sortable: true,
                    formatter: function(value, row, index) {
                        if(row.onlineStatus == 'online'){
                            return '<span class="label label-success">在线</span>';
                        }else{
                            return '<span class="label label-danger">离线</span>';
                        }
                    }
                }]
            };
            $.table.init(options);
        });

        /**
         * 获取服务器地址
         */
        function getinfo() {
            var rows = $.table.selectFirstColumns();
            $.ajax({
                url: ctx + "record/control/getinfo",
                type: "post",
                dataType: "json",
                data: {"deviceIds" : rows.join() },
                beforeSend: function (xhr, settings) {
                    var csrftoken = $('meta[name=csrf-token]').attr('content')
                    if (($.common.equalsIgnoreCase(settings.type, "POST"))) {
                        xhr.setRequestHeader("X-CSRF-Token", csrftoken)
                    }
                },
                success: function(result) {
                    $.operate.successCallback(result);
                }
            });
        }

        /**
         * 读取通道号地址
         */
        function getserver() {
            var rows = $.table.selectFirstColumns();
            layer.open({
                title: '请选择要读取的通道号',
                content: `
                        <form class="layui-form">
                          <div class="layui-form-item">
                            <select id="socketType" class="form-control select2">
                                <option value="A">通道A</option>
                                <option value="B">通道B</option>
                                <option value="C">通道C</option>
                              </select>
                          </div>
                        </form>
                        `,
                btn: ['确定'],
                // 弹层外区域关闭
                shadeClose: true,
                yes: function(index, layero) {
                    layer.close(index);
                    $.ajax({
                        url: ctx + "record/control/getserver",
                        type: "post",
                        dataType: "json",
                        data: {
                            "deviceIds" : rows.join(),
                            "socketType" : $("#socketType").val()
                        },
                        beforeSend: function (xhr, settings) {
                            var csrftoken = $('meta[name=csrf-token]').attr('content')
                            if (($.common.equalsIgnoreCase(settings.type, "POST"))) {
                                xhr.setRequestHeader("X-CSRF-Token", csrftoken)
                            }
                        },
                        success: function(result) {
                            $.operate.successCallback(result);
                        }
                    });
                },
            });
        }

        // /**
        //  * 获取服务器地址
        //  */
        // function getserver() {
        //     var rows = $.table.selectFirstColumns();
        //     $.ajax({
        //         url: ctx + "record/control/getserver",
        //         type: "post",
        //         dataType: "json",
        //         data: {"deviceIds" : rows.join() },
        //         beforeSend: function (xhr, settings) {
        //             var csrftoken = $('meta[name=csrf-token]').attr('content')
        //             if (($.common.equalsIgnoreCase(settings.type, "POST"))) {
        //                 xhr.setRequestHeader("X-CSRF-Token", csrftoken)
        //             }
        //         },
        //         success: function(result) {
        //             $.operate.successCallback(result);
        //         }
        //     });
        // }

        /**
         * 修改服务器地址
         */
        function setserver(){
            var rows = $.table.selectFirstColumns();
            var url = ctx + "record/control/setserver/" + rows.join();
            $.modal.open("修改服务器地址", url, '780', '380');
        }

        /**
         * 关闭通道
         */
        function offserver(){
            var rows = $.table.selectFirstColumns();
            layer.open({
                title: '请选择要关闭的通道号',
                content: `
                        <form class="layui-form">
                          <div class="layui-form-item">
                            <select id="socketType" class="form-control select2">
                                <option value="A">通道A</option>
                                <option value="B">通道B</option>
                                <option value="C">通道C</option>
                              </select>
                          </div>
                        </form>
                        `,
                btn: ['确定'],
                // 弹层外区域关闭
                shadeClose: true,
                yes: function(index, layero) {
                    layer.close(index);
                    $.ajax({
                        url: ctx + "record/control/offserver",
                        type: "post",
                        dataType: "json",
                        data: {
                            "deviceIds" : rows.join(),
                            "socketType" : $("#socketType").val()
                        },
                        beforeSend: function (xhr, settings) {
                            var csrftoken = $('meta[name=csrf-token]').attr('content')
                            if (($.common.equalsIgnoreCase(settings.type, "POST"))) {
                                xhr.setRequestHeader("X-CSRF-Token", csrftoken)
                            }
                        },
                        success: function(result) {
                            $.operate.successCallback(result);
                        }
                    });
                },
            });
        }

        /**
         * 程序升级
         */
        function upgrade(){
            var rows = $.table.selectFirstColumns();
            var url = ctx + "record/control/upgrade/" + rows.join();
            $.modal.open("程序升级", url, '780', '380');
        }
    </script>
</body>
<!-- 导入区域 -->
<script id="importTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10">
        <div class="col-xs-offset-1">
            <input type="file" id="file" name="file"/>
            <div class="mt10 pt5">
                <input type="checkbox" id="updateSupport" name="updateSupport" title="如果数据已经存在，更新这条数据。"> 是否更新已经存在的数据
                &nbsp;	<a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 下载模板</a>
            </div>
            <font color="red" class="pull-left mt10">
                提示：仅允许导入“xls”或“xlsx”格式文件！
            </font>
        </div>
    </form>
</script>
</html>